﻿using System.IO;
using System.Text;
using System.Net;
using System.Xml;

using ECCIT;

namespace ECCIT.Net.SAML2.Assertions
{
    public class NameIDType
    {
        // Fields
        private string format;
        private string nameID;
        private string nameQualifier;
        private string spNameQualifier;
        private string spProvidedID;

        // Properties
        public string Format
        {
            get
            {
                return this.format;
            }
            set
            {
                this.format = value;
            }
        }

        public string NameIdentifier
        {
            get
            {
                return this.nameID;
            }
            set
            {
                this.nameID = value;
            }
        }

        public string NameQualifier
        {
            get
            {
                return this.nameQualifier;
            }
            set
            {
                this.nameQualifier = value;
            }
        }

        public string SPNameQualifier
        {
            get
            {
                return this.spNameQualifier;
            }
            set
            {
                this.spNameQualifier = value;
            }
        }

        public string SPProvidedID
        {
            get
            {
                return this.spProvidedID;
            }
            set
            {
                this.spProvidedID = value;
            }
        }

        // Methods
        public NameIDType()
        {
        }

        public NameIDType(string nameID)
        {
            this.nameID = nameID;
        }

        public NameIDType(XmlElement xmlElement)
        {
            this.nameID = xmlElement.InnerText.Trim();
            if (xmlElement.HasAttribute("NameQualifier"))
            {
                this.nameQualifier = xmlElement.GetAttribute("NameQualifier").Trim();
            }
            if (xmlElement.HasAttribute("SPNameQualifier"))
            {
                this.spNameQualifier = xmlElement.GetAttribute("SPNameQualifier").Trim();
            }
            if (xmlElement.HasAttribute("Format"))
            {
                this.format = xmlElement.GetAttribute("Format").Trim();
            }
            if (xmlElement.HasAttribute("SPProvidedID"))
            {
                this.spProvidedID = xmlElement.GetAttribute("SPProvidedID").Trim();
            }
        }

        public NameIDType(string nameID, string nameQualifier, string spNameQualifier, string format, string spProvidedID)
        {
            this.nameID = nameID;
            this.nameQualifier = nameQualifier;
            this.spNameQualifier = spNameQualifier;
            this.format = format;
            this.spProvidedID = spProvidedID;
        }

        public void ToXml(XmlElement xmlElement)
        {
            xmlElement.InnerText = this.nameID;
            if (!string.IsNullOrEmpty(this.nameQualifier))
            {
                xmlElement.SetAttribute("NameQualifier", this.nameQualifier);
            }
            if (!string.IsNullOrEmpty(this.spNameQualifier))
            {
                xmlElement.SetAttribute("SPNameQualifier", this.spNameQualifier);
            }
            if (!string.IsNullOrEmpty(this.format))
            {
                xmlElement.SetAttribute("Format", this.format);
            }
            if (!string.IsNullOrEmpty(this.spProvidedID))
            {
                xmlElement.SetAttribute("SPProvidedID", this.spProvidedID);
            }
        }
    }
}
